package com.jcxy.controller;

import com.jcxy.VO.UserVo;
import com.jcxy.mapper.UserMapper;
import com.jcxy.pojo.Position;
import com.jcxy.service.PositionService;
import com.jcxy.service.UserService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userManage")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionService positionService;

    /**
     * 跳转到userEdit页面
     * @param request
     * @return
     */
    @RequestMapping("/toUserEditPage.do")
    public String toUserEditPage(HttpServletRequest request){

        //获取用户id
        Integer user_id = Integer.valueOf(request.getParameter("user_id"));
        //获取用户信息
        UserVo user = userMapper.queryUserById(user_id);
        //获取该用户所在部门的职位信息
        List<Position> positionList = positionService.queryPositionByDeptId(user.getDept_id());

        //将用户对象存储在请求中，用于前端显示
        request.setAttribute("userVo",user);
        //将用户所在部门的职位信息存储在请求中，用于前端显示
        request.setAttribute("position",positionList);
        //设置页面类型：1-修改页面
        request.setAttribute("pageType",1);

        return "userManage/userEdit";
    }

    /**
     * 通过分页获得用户列表
     * @return
     */
    @RequestMapping("/getUserListByPage.do")
    @ResponseBody
    public ResponseData getUserListByPage(HttpServletRequest request){


        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = userService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 添加用户
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/addUser.do")
    public String addUser(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        userService.insertUser(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/userManage";
    }

    /**
     * 修改用户
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateUserById.do")
    public String updateUserById(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        userService.updateUser(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/userManage";
    }

    /**
     * 通过用户id逻辑删除用户
     * @param user_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteUserById.do")
    @ResponseBody
    public ResponseData deleteUserById(Integer user_id)throws Exception{

        userService.deleteUserById(user_id);

        ResponseData ok = ResponseData.ok();
        return ok;
    }

    /**
     * 通过用户名查询用户是否存在
     * @param user_name
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryUserByName.do")
    @ResponseBody
    public ResponseData queryUserByName(String user_name)throws Exception{

        UserVo userVo = userMapper.queryUserByName(user_name);
        ResponseData ok = ResponseData.ok();
        if (userVo != null){
            ok.setCode(500);
        }
        return ok;
    }
}
